﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.Platform.Mvc.Models;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    public class MasterDataController : Controller
    {
        // GET: MasterDataDTO

        public ActionResult Index()
        {
            var root = new zTreeNode();
            var model = new MasterDataIndexModel();

            IList<MasterDataDTO> list;
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                list = proxy.Channel.GetMasterDataSort();
            }
            Build(list, root, null);

            model.LeftTree = root.Children;
            return View(model);
        }

        private void Build(IEnumerable<MasterDataDTO> table, zTreeNode node, string parentValue)
        {
            var rows = table.Where(x => x.ParentCode == parentValue).OrderBy(x => x.OrderIndex);
            foreach (var row in rows)
            {
                var _node = new zTreeNode
                {
                    Name = row.Name,
                    Id = row.Code,
                    ParentId = row.ParentCode,
                    Raw = row
                };

                node.Children.Add(_node);
                node.IsParent = node.Children.Any();
                node.Open = true;
                Build(table, _node, row.Code);
            }
        }

        [HttpGet]
        [Permission(Name = "新增", Order = 1), AutomaticTemplate]
        public ActionResult Create(string category)
        {
            var model = new MasterDataModel();

            return View(model);
        }

        [Permission(Name = "编辑", Order = 2), AutomaticTemplate]
        public ActionResult Edit(Guid id, string category)
        {
            if (string.IsNullOrEmpty(category))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            MasterDataModel vm;
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                vm = proxy.Channel.GetMasterDataInfo(id).To<MasterDataModel>();
                vm.DropDown = proxy.Channel.GetMasterData();
            }

            return View(vm);
        }

        [HttpPost]
        public ActionResult Save(MasterDataModel model)
        {
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.SaveMasterData(model.To<MasterDataDTO>());

                return this.JsonNet(res);
            }
        }

        #region 分页数据

        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                var pageInfo = proxy.Channel.MasterDataGetPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }

        #endregion

        [HttpPost]
        [Permission(Name = "删除", Order = 3)]
        public ActionResult Delete(Guid[] ids)
        {
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.DeleteMasterData(ids);

                return this.JsonNet(res);
            }

        }
    }
}